class OrderStateMachine:
    def __init__(self):
        self.states = {
            0: "待支付",
            1: "已支付",
            2: "已派给师傅",
            3: "师傅已接单",
            4: "维修中",
            5: "已完成"
        }
        self.current_state = 0  # 初始状态为待支付
        self.state_sequence = list(self.states.keys())
        
    def next(self):
        """顺序切换到下一个状态"""
        if self.current_state == 5:
            print("错误：已是最终状态，无法继续切换")
            return False
        
        new_state = self.current_state + 1
        print(f"状态切换: {self.states[self.current_state]} → {self.states[new_state]}")
        self.current_state = new_state
        return True
    
    def go_to_state(self, state_code):
        """切换到指定状态（支持顺序和逆序）"""
        if state_code not in self.states:
            print(f"错误：无效状态码 {state_code}")
            return False
            
        if state_code == self.current_state:
            print(f"警告：已是状态 {self.states[state_code]}，无需切换")
            return False
            
        if state_code < self.current_state:
            # 逆序切换
            return self._go_backward(state_code)
        else:
            # 顺序切换（逐步执行）
            return self._go_forward(state_code)
    
    def _go_forward(self, target_state):
        """顺序逐步切换到目标状态"""
        while self.current_state < target_state:
            if not self.next():
                return False
        return True
    
    def _go_backward(self, target_state):
        """逆序切换到目标状态"""
        print(f"状态切换: {self.states[self.current_state]} ← {self.states[target_state]} (逆序)")
        self.current_state = target_state
        return True
    
    def get_current_state(self):
        """获取当前状态信息"""
        return self.current_state, self.states[self.current_state]
    
    def __str__(self):
        state_code, state_name = self.get_current_state()
        return f"当前状态: [{state_code}]{state_name}"
